package com.xxx.es;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * 初始化EsClient
 */
public class EsClientBuilder {
	
	private String host;
	private String port;

	public void setHost(String host) {
		this.host = host;
	}
	public void setPort(String port) {
		this.port = port;
	}

	public RestHighLevelClient init() {
		String[] hostArr = host.split(",");
		String[] portArr = port.split(",");
		HttpHost[] hosts = new HttpHost[hostArr.length];
		for (int i = 0; i < hosts.length; i++) {
			HttpHost esServer = new HttpHost(hostArr[i], Integer.valueOf(portArr[i]));
			hosts[i] = esServer;
		}
		return new RestHighLevelClient(RestClient.builder(hosts)); // RestHighLevelClient也是基于RestClient构建的
	}
}
